package com.aiyige.page.login;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiyige.BuildConfig;
import com.aiyige.R;
import com.aiyige.arouter.config.ARouterConfig;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.eventbus.EventAgreement;
import com.aiyige.base.eventbus.EventLogin;
import com.aiyige.model.User;
import com.aiyige.page.login.callbacks.ITimerCallBack;
import com.aiyige.page.login.presenter.BindingPhonePresenter;
import com.aiyige.page.login.view.IBindingPhoneView;
import com.aiyige.utils.VerifyCodeManager;
import com.aiyige.utils.widget.VerifyCodeInputView;
import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

@Route(path = ARouterConfig.BindingPhonePage)
public class BindingPhonePage extends BaseActivity implements IBindingPhoneView, ITimerCallBack {
    public static final String TAG = BindingPhonePage.class.getSimpleName();
    @BindView(R.id.tv_county_name)
    TextView tvCountyName;
    @BindView(R.id.tv_county_number)
    TextView tvCountyNumber;
    @BindView(R.id.rl_select_county)
    RelativeLayout rlSelectCounty;
    @BindView(R.id.et_username)
    EditText etUsername;
    @BindView(R.id.widget_verify_code)
    VerifyCodeInputView widgetVerifyCode;
    @BindView(R.id.btn_send_verify_code)
    Button btnSendVerifyCode;
    @BindView(R.id.btn_next)
    Button btnNext;
    @BindView(R.id.iv_agreement)
    ImageView ivAgreement;
    @Autowired
    public User.ThirdOauthBackup thirdOauthBackup;

    boolean isAgreement = true;

    private BindingPhonePresenter presenter = new BindingPhonePresenter(this);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.page_binding_phone);
        ButterKnife.bind(this);
        ARouter.getInstance().inject(this);
        setAutoRegisterEventBus(true);
        etUsername.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                checkInput();
            }
        });
        widgetVerifyCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                checkInput();
            }
        });
        VerifyCodeManager.getInstance().addLoginCallBack(this);
        btnNext.setEnabled(false);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onPaySuccessEventBuss(EventLogin event) {
        finish();
    }



    private void checkInput() {
        boolean isNull = true;
        if (!isEmpty(getUserName()) && !isEmpty(getVerifyCode())
                && getVerifyCode().length() == 6 && isAgreement) {
            isNull = false;
        }
        btnNext.setEnabled(!isNull);
    }

    @Override
    public String getUserName() {
        return etUsername.getText().toString().trim();
    }

    @Override
    public String getVerifyCode() {
        return widgetVerifyCode.getVerifyCode();
    }

    @Override
    public User.ThirdOauthBackup getThirdOauthBackup() {
        return thirdOauthBackup;
    }

    @Override
    public void setVerifyCode(String verifyCode) {
        widgetVerifyCode.setVerifyCode(verifyCode);
    }

    @Override
    public void showLoading() {
        super.showLoading();
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
    }

    @Override
    public Context getMyContext() {
        return this;
    }


    @Override
    public void onTick(long l) {
        //防止计时过程中重复点击
        btnSendVerifyCode.setClickable(false);
        btnSendVerifyCode.setText((l / 1000) + "s");
        btnSendVerifyCode.setTextColor(ContextCompat.getColor(this, R.color.zwcolor_gray_b1b5c1));

    }

    @Override
    public void onFinish() {
        btnSendVerifyCode.setTextColor(ContextCompat.getColor(this, R.color.zwcolor_black_111111));
        //重新给Button设置文字
        btnSendVerifyCode.setText(getResources().getString(R.string.get_verification_code));
        //设置可点击
        btnSendVerifyCode.setClickable(true);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        VerifyCodeManager.getInstance().removeLoginCallBack(this);
    }

    @OnClick({R.id.rl_back, R.id.iv_earth, R.id.tv_county_name, R.id.iv_right, R.id.tv_county_number,
            R.id.rl_select_county, R.id.btn_send_verify_code, R.id.btn_next, R.id.rl_agreement, R.id.iv_agreement
            , R.id.tv_agreement})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            case R.id.iv_earth:
            case R.id.tv_county_name:
            case R.id.iv_right:
            case R.id.tv_county_number:
            case R.id.rl_select_county:
//                ARouter.getInstance().build(ARouterConfig.SelectCountryPage).navigation(this,COUNTRY_REQUEST_CODE);
                break;
            case R.id.btn_send_verify_code:
                presenter.sendVerifyCode();
                break;
            case R.id.btn_next:
                presenter.next(BindingPhonePage.this);
                break;
            case R.id.tv_agreement:
                ARouter.getInstance()
                        .build(ARouterConfig.WebViewPage)
                        .withString("url", BuildConfig.DEBUG ? "http://m.test.dance365.com/agreement.html" :
                                "https://m.dance365.com/agreement.html")
                        .withBoolean("noTitleBar", false)
                        .withBoolean("disableNav", false)
                        .withString("fixedTitle",getString(R.string.china_dance_user_agreement))
                        .navigation();
                break;
            case R.id.rl_agreement:
            case R.id.iv_agreement:
                isAgreement = !isAgreement;
                ivAgreement.setImageResource(isAgreement ? R.drawable.login_agreed : R.drawable.login_disagree);
                checkInput();
                break;
        }
    }
}
